<template>
  <div class="">
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState">
        <a-form-item name="zjm" class="w-[300px]">
          <a-input v-model:value="formState.zjm" placeholder="设备名称/助记码/装备编号/设备序列号" />
        </a-form-item>
        <a-form-item name="dateRange" label="创建时间" class="w-[300px] ml-4">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" :allow-clear="false" value-format="YYYY-MM-DD" @change="data" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <div class="flex">
        <a-button class="mb-2 mr-2" type="primary" @click="onCLickEdit"> 新增 </a-button>
        <BaseExport type="战救" name="装备调出" code="000608" :search-params="formState">导出</BaseExport>
        <!-- <a-button @click="onPrint">打印</a-button> -->
        <a-button v-print="{ id: 'printMe' }" class="ml-2">打印</a-button>
        <div class="absolute top-[-9999px] hidden">
          <PrintView id="printMe" :data-list="tableData" />
        </div>
      </div>
      <BaseTable :data-source="tableData" fill :loading="loading" column-code="000608" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'NO'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'status'">
            <a-tag :color="statusEnum[record.status].color">{{ statusEnum[record.status].text }}</a-tag>
          </template>

          <template v-if="column.dataIndex === 'equipmentName'">
            <MedicineInfo :value="{ mc: record.equipmentName, gg: record.specifications, cj: record.manufacturer }" />
          </template>

          <template v-if="column.dataIndex === 'action'">
            <a-space>
              <BaseButton type="link" size="small" :disabled="record.status !== '0'" @click="onCLickEdit(record)"> 修改 </BaseButton>
              <BaseButton v-if="record.status === '0'" type="link" size="small" :disabled="record.status !== '0'" @click="onCLickStatus(record, '1')">
                审核
              </BaseButton>
              <BaseButton
                v-else-if="record.status === '1'"
                confirm-text="确认撤审？"
                type="link"
                danger
                size="small"
                :disabled="record.status !== '1'"
                @click="onCLickStatus(record, '0')"
              >
                撤审
              </BaseButton>
              <BaseButton type="link" size="small" :disabled="record.status !== '1'" @click="onCLickStatus(record, '2')"> 确认 </BaseButton>
              <BaseButton confirm-text="确认删除？" type="link" danger size="small" :disabled="record.status !== '0'" @click="onCLickDel(record)">
                删除
              </BaseButton>
            </a-space>
          </template>
        </template>
      </BaseTable>
    </a-card>
    <Edit v-model:open="editOpen" :row-data="rowData" @success="getData()" />
  </div>
</template>

<script setup>
import { onMounted, ref } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'
import { post } from '@/utils/request'
import BaseTable from '@/components/BaseTable'
import Edit from './Edit.vue'
import PrintView from './PrintView.vue'

const editOpen = ref(false)
const rowData = ref({})
const formRef = ref(null)
const statusEnum = [
  { text: '未审核', color: 'red' },
  { text: '已审核', color: 'green' },
  { text: '已确认', color: 'blue' }
]

const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
})

const onCLickStatus = async (record, type) => {
  const res = await post('/war/equipmentScrap/doUpdateEquipmentScrap', {
    id: record.id,
    status: type
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
const onCLickDel = async (record) => {
  const res = await post('/war/equipmentTransfer/doDelEquipmentTransfer', {
    id: record.id
  })
  if (res.code !== 0) return message.error(res.msg)
  message.success(res.msg)
  getData()
}
// 打印
// const onPrint = async () => {
//   const { code, msg, data } = await post('/inp/Print/hospitalizationNoticesPrint', {})
//   if (code !== 0) return message.error(msg)
//   window.openExe?.rpt(data) //调用winform打印
// }
const { loading, tableData, getData, pagination } = useTable({
  url: '/war/equipmentScrap/getEquipmentScrapList',
  immediate: true,
  formatParams: () => {
    return {
      startDate: formState.value.dateRange?.[0],
      endDate: formState.value.dateRange?.[1],
      ...formState.value
    }
  }
})
const onCLickEdit = (record = {}) => {
  rowData.value = record
  editOpen.value = true
}

onMounted(() => {
  getData()
})
</script>
